<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <el-menu
            :default-active="1"
            class="el-menu-demo"
            mode="horizontal"
            background-color="red"
            text-color="#fff"
            :ellipsis="false"
            active-text-color="#ffd04b"
        >
          <el-menu-item index="1">
            <div style="display: flex;align-items: center;">
              <i class="iconfont icon-jingdong"
                 style="background-color: red;font-size: 40px;color:white;margin: 30px;"></i>
              <h3 style="color: white;">京东商城</h3>
            </div>
          </el-menu-item>
          <el-sub-menu index="2"  style=" ">
            <template #title >{{admin.username}}</template>
            <el-menu-item index="2-1" @click="updateDialogShow=true" style=" --el-menu-active-color: #fff; --el-menu-hover-text-color: #ffd04b">个人中心</el-menu-item>
            <el-menu-item index="2-2" @click="chgPwdDialogShow=true" style=" --el-menu-active-color: #fff; --el-menu-hover-text-color: #ffd04b">修改密码</el-menu-item>
            <el-menu-item index="2-3" @click="logout" style=" --el-menu-active-color: #fff; --el-menu-hover-text-color: #ffd04b">退出</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu active-text-color="#ffd04b" background-color="#000000" class="el-menu-vertical-demo"
                   default-active="2"
                   text-color="#ffffff" :router="true">
            <el-menu-item index="/admin/index">
              <el-icon>
                <HomeFilled/>
              </el-icon>
              后台首页
            </el-menu-item>
            <el-sub-menu index="1">
              <template #title>
                <el-icon>
                  <UserFilled/>
                </el-icon>
                <span>管理员管理</span>
              </template>
              <el-menu-item index="/admin/admin">管理员列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="2">
              <template #title>
                <el-icon>
                  <Grid/>
                </el-icon>
                <span>商品分类管理</span>
              </template>
              <el-menu-item index="/admin/category">分类列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="3">
              <template #title>
                <el-icon>
                  <Goods/>
                </el-icon>
                <span>商品管理</span>
              </template>
              <el-menu-item index="/admin/goods">商品列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="4">
              <template #title>
                <el-icon>
                  <Avatar/>
                </el-icon>
                <span>用户管理</span>
              </template>
              <el-menu-item index="/admin/user">用户列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="5">
              <template #title>
                <el-icon>
                  <List/>
                </el-icon>
                <span>订单管理</span>
              </template>
              <el-menu-item index="/admin/order">订单列表</el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main>
          <RouterView/>
        </el-main>
      </el-container>
    </el-container>
  </div>

  <!-- 修改密码开始 -->
  <el-dialog v-model="chgPwdDialogShow" title="修改密码" width="500">
    <el-form>
      <el-form-item label="原密码" label-width="20%">
        <el-input type="password" v-model="chgPwdObj.oldPwd" placeholder="请输入原密码" autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item  label="新密码" label-width="20%">
        <el-input type="password" v-model="chgPwdObj.newPwd" placeholder="请输入新密码" autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item label="确认新密码" label-width="20%">
        <el-input type="password" v-model="chgPwdObj.newPwd1" placeholder="请输入确认新密码" autocomplete="off" style="width: 300px"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="chgPwdDialogShow = false">
          取消
        </el-button>
        <el-button type="primary" @click="chgPwd">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 修改密码结束 -->

  <!-- 修改管理员信息开始 -->
  <el-dialog v-model="updateDialogShow" title="修改管理员个人信息" width="500">
    <el-form>
      <el-form-item label="用户名" label-width="20%">
        <el-input  v-model="admin.username" disabled  autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item  label="电话" label-width="20%">
        <el-input  v-model="admin.phone"  placeholder="请输入电话" autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item label="邮箱" label-width="20%">
        <el-input  v-model="admin.email" placeholder="请输入邮箱" autocomplete="off" style="width: 300px"/>
      </el-form-item>
      <el-form-item label="真实姓名" label-width="20%">
        <el-input  v-model="admin.realname" placeholder="请输入真实姓名" autocomplete="off" style="width: 300px"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="updateDialogShow = false">
          取消
        </el-button>
        <el-button type="primary" @click="chgInfo">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 修改管理员信息结束 -->

</template>

<script setup>
import {RouterView} from "vue-router";
import router from "@/router/index.js";
import {useTokenStore} from "@/stores/token.js";
import adminApi from "@/api/adminApi.js";
import {ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {Plus} from "@element-plus/icons-vue";

const tokenStore = useTokenStore();
const admin = ref({
  username:null,
  phone:null,
  email:null,
  realname:null,
})
//是否显示修改密码的对话框
const chgPwdDialogShow = ref(false);
//保存修改信息的对象
const chgPwdObj = ref({
  oldPwd:null,
  newPwd:null,
  newPwd1:null
})
//是否显示修改管理员信息的对话框
const updateDialogShow = ref(false);

//修改个人信息
function chgInfo(){
  adminApi.chgInfo(admin.value)
      .then(resp => {
          if(resp.code == 10000){
            ElMessage.success(resp.msg);
            updateDialogShow.value = false;
            refreshCurrentPage();
          }else {
            ElMessage.error(resp.msg);
          }
      })
}

//修改密码
function chgPwd(){
  adminApi.chgPwd(chgPwdObj.value)
      .then(resp => {
        if(resp.code == 10000){
          chgPwdObj.value={
            oldPwd:null,
            newPwd:null,
            newPwd1:null
          }
          ElMessage.success({
            message: resp.msg,
            duration: 1000,
            onClose: () => {
              //重置token并退出重新登陆
              tokenStore.$reset();
              //跳转到管理员登录页
              router.push("/admin/login");
            }
          })
        }else {
          ElMessage.error(resp.msg);
        }
        getInfo();
      });
}

//获取登录用户信息
function getInfo(){
  adminApi.getInfo()
      .then(resp => {
        admin.value = resp.data;
      })
}

//退出
function logout() {
  ElMessageBox.confirm(
      '确认退出该账户?',
      '退出',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        ElMessage.success({
          message: `退出成功`,
          duration: 1000,
          onClose: () => {
            tokenStore.$reset();
            //跳转到管理员登录页
            router.push("/admin/login");
          }
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消退出',
          duration: 1000
        })
      })

}
//刷新界面
function refreshCurrentPage() {
  location.reload();
}

getInfo();
</script>

<style scoped>
.el-menu--horizontal > .el-menu-item:nth-child(1) {
  margin-right: auto;
}

.el-header {
  padding: 0px;
}
.common-layout, .el-container, ul {
  height: 100%;
}

</style>
